<?php

namespace App\Model;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

/**
 * App\Model\UserInterPushInfo
 *
 * @mixin \Eloquent
 * @property int $id
 * @property int|null $user_id
 * @property int|null $material_id 素材id
 * @property string|null $material_type 素材类型， 直接记录表明
 * @property int|null $province_id 推广区域_id
 * @property string|null $province 推广区域
 * @property int|null $num 允许匹配次数
 * @property int $use_num 使用次数
 * @property int|null $is_enabled 启用0， 1禁用
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereIsEnabled($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereMaterialId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereMaterialType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereUseNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereUserId($value)
 * @property-read \App\User|null $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo query()
 * @property-read \App\Model\UserInterPushRecordMaterial $interPushRecordMaterial
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Model\UserInterPushRecord[] $interPushRecord
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $material
 * @property int $others_browse_num 为他人带来浏览次数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereOthersBrowseNum($value)
 * @property int|null $material_style 1,文章，2个人网页，3营销网页， 4名片
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereMaterialStyle($value)
 * @property int|null $forme_brows_num 为我带来的浏览次数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterPushInfo whereFormeBrowsNum($value)
 */
class UserInterPushInfo extends Model
{
    protected $table = "user_interpush_info";

    /**
     * 互推圈信息属于哪个用户
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
       return $this->belongsTo(User::class, "user_id", "id");
    }


    /**
     * 获取主动信息
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function  interPushRecord()
    {
        return $this->hasMany(UserInterPushRecord::class, "interpush_id","id");
    }

    /**
     * 获取被动信息
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function passiveInterPushRecord()
    {
        return $this->hasMany (UserInterPushRecord::class, "target_interpush_id","id");
    }


    /**
     * 素材
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function material()
    {
        return $this->morphTo();
    }

    /**
     * 统计互推圈使用次数
     * @param $regexp
     * @return int
     */
    public static function interPushNum($regexp)
    {
        $grade_user_count = UserUp::whereRaw ("user_up_list REGEXP ?", [$regexp])->count();
        switch ($grade_user_count){
            case $grade_user_count< 5:
                return 2;
                break;
            case $grade_user_count >5 && $grade_user_count <10:
                return  3;
                break;
            case $grade_user_count >10:
                return 4;
                break;
        }
        return 0;
    }

}
